﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Phone.Controls;
using Microsoft.Unsupported;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
using com.google.zxing;
using System.Text;

namespace iLikeWine
{
    public partial class MainPage : PhoneApplicationPage
    {
        BitmapImage tintoImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_tinto.png", UriKind.Relative));
        BitmapImage brancoImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_branco.png", UriKind.Relative));
        BitmapImage roseImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_rose.png", UriKind.Relative));
       
        Uri targetPage;
        private const string conn = @"isostore:/iLikeWine.sdf";
        Wine w = new Wine();
        private static string selecao;

        object selectedItem;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
           
            //--> BD_iLikeWine é criada caso não exista
            using (var ctx = new iLikeWineDataContext(conn))
            {
                if (!ctx.DatabaseExists())
                {
                    // cria dB caso não exista
                    ctx.CreateDatabase();
                }
            }
            //<--
            IList<Wine> lista = this.GetWines();


            foreach (Wine w in lista)
            {


                
                    if (w.Tipo.Equals("Tinto"))
                    {


                        w.ImageSource = tintoImage;

                    }
                    else if (w.Tipo.Equals("Branco"))
                    {

                        w.ImageSource = brancoImage;

                    }
                    else
                    {

                        w.ImageSource = roseImage;
                    }
                
            }     

            listBox1.ItemsSource = from Wine w in lista
            select new Wine
            {
                ImageSource = w.ImageSource,
                Nome = w.Nome,
                Ano = w.Ano
            };

        }

        
        public IList<Wine> GetWines()
        {
            IList<Wine> lista = null;
            using (var ctx = new iLikeWineDataContext(conn))
            {
                IQueryable<Wine> query = ctx.Wines;
                //IQueryable<Wine> query = from ctx in ctx.Wines select w ;
                lista = query.ToList();
            }

            return lista;
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/insert_new_wine.xaml",UriKind.RelativeOrAbsolute));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/qrcode_search.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PivotVerVinhos.xaml", UriKind.RelativeOrAbsolute));
        }

   

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/email_send.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/creditos.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Ajuda.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PivotVerVinhos.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/FindWine.xaml", UriKind.RelativeOrAbsolute)); 
        }

        public static string ListSelect
        {
            get { return selecao; }
            set { selecao = value; }
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
               
                selectedItem = (Wine)e.AddedItems[0];
                targetPage = new Uri("/Ver_CodBar_Search.xaml", UriKind.Relative);

                // reset selecção da ListBox
                ((ListBox)sender).SelectedIndex = -1;

                // Muda de pagina
                NavigationService.Navigate(targetPage);
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                root.DataContext = selectedItem;
            }
        }

        private void Tap_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel rect = sender as StackPanel;
            // Change the size of the Rectangle.
            if (null != rect)
            {
                rect.Opacity = 0.5;


            }
        }
        private void Tap_LeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            StackPanel rect = sender as StackPanel;
            // Reset the dimensions on the Rectangle.
            if (null != rect)
            {
                rect.Opacity = 1.0;

            }
        }
        private void Tap_MouseLeave(object sender, MouseEventArgs e)
        {
            StackPanel rect = sender as StackPanel;
            // Finger moved out of Rectangle before the mouse up event.
            // Reset the dimensions on the Rectangle.


            if (null != rect)
            {
                rect.Opacity = 1.0;

            }
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.RemoveBackEntry();
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button8_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/ver_wines.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}